export default{
	state:{
		ceshi:"测试",
		ceshi2:"ceshi2",
		//录音管理器
		RECORD:null,
		recordtime:0,
		recordTimer:null,
		events:[],
		sendVoice:null,
	},
	mutations:{
		//全局录音管理器
		initRecord(state){
			state.RECORD=uni.getRecorderManager();
			//监听录音开始
			state.RECORD.onStart(e=>{
				state.recordtime=0
				state.recordTimer=setInterval(()=>{
					state.recordtime++;
				},1000)
			})
			//监听录音结束
			state.RECORD.onStop(e=>{
				if(state.recordTimer){
					clearInterval(state.recordTimer)
				}
				// state.sendVoice();
				console.log(e,"onstop");
				if(typeof state.sendVoice == 'function'){
					state.sendVoice(e.tempFilePath)
				}
				// if(!state.unrecord){
				// 	let time = (new Date()).getTime();
				// 	let obj = {
				// 		avater: "https://profile-avatar.csdnimg.cn/3365568353cd44c2acf217ab8b6345f9_weixin_47365777.jpg!1",
				// 		user_id: 1,
				// 		nickname: "亚刻",
				// 		type: "audio", //image,audio,video
				// 		options: {
				// 			time: this.recordtime
				// 		},
				// 		create_time: time,
				// 		data: e.tempFilePath,
				// 		isremove: false,
					
				// 	}
					
				// 	this.list.push(obj)
					
				// 	this.pageToBottom()
				// }
				
			})
		},
		//注册音频事件
		regSendVoiceEvent(state,event){
			state.sendVoice=event
			
		},
		//注册全局事件
		regEvent(state,event){
			console.log("注册全局事件");
			state.events.push(event)
			
		},
		doEvent(state,params){
			console.log("执行全局事件",state.events);
			state.events.forEach(e=>{
				e(params)
			})
		},
		removeEvent(state,event){
			let index=state.events.findIndex(item=>{
				return item===event
			})
			console.log(index,"index");
			if(index !== -1){
				state.events.splice(index,1)
			}
		}
	},
	actions:{
		//分发注册全局事件
		audioon({commit},event){
			commit('regEvent',event)
			
		},
		//分发执行全局事件
		audioemit({commit},params){
			commit('doEvent',params)
		},
		//分发注销
		audiooff({commit},event){

commit('removeEvent',event)			
		}
	}
}